package cn.shujuhai.cms.service.thymeleaf;


import cn.shujuhai.cms.domain.SysChannel;
import cn.shujuhai.cms.mapper.SysChannelMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: zhangDeZhong
 * @Date: 2024/12/5 12:01
 * @Description: 提供模板中栏目的工具类
 **/
@Component
@RequiredArgsConstructor
public class ChannelTool {
    private final SysChannelMapper sysChannelMapper;

    /**
     * 根据栏目ID获取栏目对象
     *
     * @param channelId 栏目ID
     * @return 栏目信息
     */
    public SysChannel fetchBase(Long channelId) {
        return this.sysChannelMapper.selectById(channelId);
    }

    /**
     * 根据栏目ID获取父栏目
     *
     * @param parentId 栏目ID
     * @return 栏目信息
     */
    public SysChannel fetchParentChannel(Long parentId) {
        return this.sysChannelMapper.selectOne(Wrappers.<SysChannel>lambdaQuery()
                .eq(SysChannel::getChannelId, parentId));
    }

    /**
     * 根据栏目ID获取子栏目
     *
     * @param channelId 栏目ID
     * @return 栏目列表
     */
    public List<SysChannel> fetchChildChannel(Long channelId) {
        return this.sysChannelMapper.selectList(Wrappers.<SysChannel>lambdaQuery()
                .eq(SysChannel::getParentId, channelId));
    }

    /**
     * 获取栏目菜单列表
     *
     * @return 栏目列表
     */
    public List<SysChannel> fetchList() {
        return this.sysChannelMapper.selectList(Wrappers.<SysChannel>lambdaQuery()
                .eq(SysChannel::getParentId, 0)
                .eq(SysChannel::getKind, 0)
                .orderByAsc(SysChannel::getChannelSort));
    }

    /**
     * 根据栏目ID获取第一个子栏目
     *
     * @param channelId 栏目ID
     * @return 栏目信息
     */
    public SysChannel fetchFirstChildChannel(Long channelId) {
        return this.sysChannelMapper.selectList(Wrappers.<SysChannel>lambdaQuery()
                .eq(SysChannel::getParentId, channelId)
                .orderByAsc(SysChannel::getChannelSort).last("limit 1")).stream().findFirst().orElse(null);
    }
}
